package com.example.itshome.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.itshome.R;
import com.example.itshome.impl.ViewOutlineProviderImpl;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.ClockInRequest;
import com.example.itshome.util.TimeUtil;
import com.zhpan.bannerview.BannerViewPager;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class MainPageBannerItemFragment extends Fragment implements UserInfoManager.OnUpdateUserRunner {

    private ImageView backgroundImageView;
    private final TextView[] mTvSignIn=new TextView[4];
    private Button clockInButton;
    private TextView dayTextView;
    private TextView monthTextView;
    private TextView nameAndPlaceTextView;
    private TextView introductionTextView;
    private TextView clockedInShowTextView;
    private ImageView clockedInShowImageView;
    private View clockedInInVisibleLayout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_banner_item2, container, false);
        initFind(view);
        Bundle bundle=getArguments();
        if (bundle != null) {
            setBackgroundImageViewSrc(bundle.getInt("bgResourceId"));
            setDescription(bundle.getStringArray("description"));
        }
        initTextView();
        initButton();
        initClockView();
        UserInfoManager.getInstance().addRunners(this);
        return view;
    }

    void initFind(View v){
        mTvSignIn[0]=v.findViewById(R.id.txv_main_page_banner_item2_sign1);
        mTvSignIn[1]=v.findViewById(R.id.txv_main_page_banner_item2_sign2);
        mTvSignIn[2]=v.findViewById(R.id.txv_main_page_banner_item2_sign3);
        mTvSignIn[3]=v.findViewById(R.id.txv_main_page_banner_item2_sign4);
        clockInButton=v.findViewById(R.id.btn_main_page_banner_item2_clock_in);
        dayTextView=v.findViewById(R.id.txv_main_page_banner_item2_day);
        monthTextView=v.findViewById(R.id.txv_main_page_banner_item2_month);
        backgroundImageView=v.findViewById(R.id.imv_main_page_banner_item2_background);
        nameAndPlaceTextView=v.findViewById(R.id.txv_main_page_banner_item2_name_place);
        introductionTextView=v.findViewById(R.id.txv_main_page_banner_item2_introduction);
        clockedInShowTextView=v.findViewById(R.id.txv_main_page_clock_in_success);
        clockedInShowImageView=v.findViewById(R.id.imv_main_page_clock_in_cat);
        clockedInInVisibleLayout=v.findViewById(R.id.layout_main_page_clock_in_all_invisible_view);
    }

    void setBackgroundImageViewSrc(int resourceId){
        backgroundImageView.setImageResource(resourceId);
        backgroundImageView.setOutlineProvider(new ViewOutlineProviderImpl(50));
        backgroundImageView.setClipToOutline(true);
    }

    void setDescription(String[] info){
        nameAndPlaceTextView.setText(info[0]);
        introductionTextView.setText(info[1]);
    }


    void initTextView(){
        mTvSignIn[0].setSelected(true);
        dayTextView.setText(TimeUtil.format(new Date(),"dd"));
        monthTextView.setText(String.valueOf(TimeUtil.getMonthStr(LocalDate.now())+" "+TimeUtil.getDayOfWeekStr(LocalDate.now())));
    }

    void clockedInChange(){
        clockedInInVisibleLayout.setVisibility(View.INVISIBLE);
        clockedInShowImageView.setVisibility(View.VISIBLE);
        clockedInShowTextView.setVisibility(View.VISIBLE);
    }


    void initButton(){
        clockInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Runnable run=new Runnable() {
                    @Override
                    public void run() {
                        clockedInChange();
                    }
                };
                AbstractNetworkRequest request=new ClockInRequest(getActivity())
                        .setRun(run);
                request.start();
            }
        });
    }

    void initClockView(){
        int day=UserInfoManager.getInstance().getUser().getClockInCount();
        mTvSignIn[0].setText(String.valueOf("累计"+day+"天"));
        for(int i=1;i<4;i++){
            mTvSignIn[i].setText(String.valueOf((i+day)+"天"));
        }
    }

    @Override
    public void onInfoUpdate() {
        initClockView();
    }
}